package com.admin.system.controller;

import com.admin.base.BaseController;
import com.admin.base.Page;
import com.admin.base.Response;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.admin.system.entity.SystemRole;
import com.admin.system.entity.SystemUser;
import com.admin.system.service.SystemRoleService;
import com.admin.system.service.SystemUserService;
import com.admin.system.vo.query.SystemRoleQuery;

@Controller
@RequestMapping(value = "/system/role")
public class SystemRoleController extends BaseController {
	@Autowired
	SystemRoleService roleService;
	@Autowired
	SystemUserService systemUserService;

	/**
	 * 列表页面
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
		mav.setViewName("system/systemRole/list");
		return mav;
	}

	/**
	 * 分页查询数据
	 */
	@RequestMapping(value = "/paging")
	@ResponseBody
	public Response paging(HttpServletRequest request, HttpServletResponse response, SystemRoleQuery query) {
		Page<SystemRole> page = roleService.paging(query);
		return Response.ok(page);
	}

	/**
	 * 新增页面
	 */
	@RequestMapping(value = "/toAdd")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
		mav.setViewName("system/systemRole/add");
		return mav;
	}

	/**
	 * 保存数据
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Response add(HttpServletRequest request, HttpServletResponse response, SystemRole systemRole) {
		roleService.add(systemRole);
		return Response.ok();
	}

	/**
	 * 输出数据
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Response delete(HttpServletRequest request, HttpServletResponse response, Integer[] id) {
		roleService.delete(id);
		return Response.ok();
	}

	/**
	 * 编辑页面
	 */
	@RequestMapping(value = "/toEdit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, Integer id, ModelAndView mav) {
		SystemRole systemRole = roleService.queryById(id);
		mav.addObject(systemRole);
		mav.setViewName("system/systemRole/edit");
		return mav;
	}

	/**
	 * 详情页面
	 */
	@RequestMapping(value = "/detail")
	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, Integer id, ModelAndView mav) {
		SystemRole systemRole = roleService.queryById(id);
		mav.addObject(systemRole);
		mav.setViewName("system/systemRole/detail");
		return mav;
	}

	/**
	 * 更新数据
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Response update(HttpServletRequest request, HttpServletResponse response, SystemRole systemRole) {
		roleService.update(systemRole);
		return Response.ok();
	}

	@RequestMapping(value = "/updateStatus")
	@ResponseBody
	public Response updateStatus(HttpServletRequest request, HttpServletResponse response, SystemRole systemRole) {
		roleService.updateStatus(systemRole);
		return Response.ok();
	}

	/**
	 * 角色选取页面
	 */
	@RequestMapping(value = "/toAssign")
	public ModelAndView check(HttpServletRequest request, HttpServletResponse response, ModelAndView mav,
			Integer userId) {
		mav.setViewName("system/systemUser/assign");
		Object zNodes = JSON.toJSON(roleService.assignedRole(userId));
		SystemUser user = systemUserService.queryById(userId);
		mav.addObject("zNodes", zNodes);
		mav.addObject("user", user);
		return mav;
	}

	@RequestMapping(value = "/assign")
	@ResponseBody
	public Response assign(HttpServletRequest request, HttpServletResponse response, Integer roleId, Integer userId) {
		roleService.assignRole(userId, roleId);
		return Response.ok();
	}

}
